﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsAppTP
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
         
    }
        private string curFilename;
        private System.Drawing.Bitmap curbitmap;
        Stopwatch sw = new Stopwatch();
        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDlg = new OpenFileDialog();
            openFileDlg.Title = "选择文件";
            openFileDlg.Multiselect = false;//是否可以多选
            openFileDlg.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);//设置默认路径为桌面
            openFileDlg.Filter = "图片文件|*.jpg|BMP图片|*.bmp|Gif图片|*.gif"; //设置多文件格式

            curFilename = openFileDlg.FileName;//地址
            if (openFileDlg.ShowDialog() == DialogResult.OK)
            {
                String filePath = openFileDlg.FileName;//设置路径
                this.pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
                this.pictureBox1.ImageLocation = filePath;//选择照片后在PictureBox中显示
                try
                {
                    curbitmap = (Bitmap)Image.FromFile(curFilename);//将公共变量curbitmap赋值
                }
                catch (Exception exp)
                {
                    MessageBox.Show(exp.Message);//如果出错显示错误信息
                }

            }
            //计时器
            sw.Start();
            //负片，将右下减去左上在加128
            try
            {
                int Height = this.pictureBox1.Image.Height;
                int Width = this.pictureBox1.Image.Width;
                Bitmap newBitmap = new Bitmap(Width, Height);
                Bitmap oldBitmap = (Bitmap)this.pictureBox1.Image;
                Color pixel1, pixel2;
                for (int x = 0; x < Width - 1; x++)
                {
                    for (int y = 0; y < Height - 1; y++)
                    {
                        int r = 0, g = 0, b = 0;
                        pixel1 = oldBitmap.GetPixel(x, y);
                        pixel2 = oldBitmap.GetPixel(x + 1, y + 1);
                        r = Math.Abs(pixel1.R - pixel2.R + 128);
                        g = Math.Abs(pixel1.G - pixel2.G + 128);
                        b = Math.Abs(pixel1.B - pixel2.B + 128);
                        if (r > 255)
                            r = 255;
                        if (r < 0)
                            r = 0;
                        if (g > 255)
                            g = 255;
                        if (g < 0)
                            g = 0;
                        if (b > 255)
                            b = 255;
                        if (b < 0)
                            b = 0;
                        newBitmap.SetPixel(x, y, Color.FromArgb(r, g, b));
                    }
                }
                //显示处理后的图片
                this.pictureBox2.SizeMode = PictureBoxSizeMode.StretchImage;//将图片处理后的大小显示在框中
                this.pictureBox2.Image = newBitmap;
                //显示运行时间
                sw.Stop();
                label1.Text ="运行时间：" +sw.ElapsedMilliseconds.ToString()+"毫秒";
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }


        }

        private void button2_Click(object sender, EventArgs e)
        {
            //获取图片
            OpenFileDialog openFileDlg = new OpenFileDialog();
            openFileDlg.Title = "选择文件";
            openFileDlg.Multiselect = false;//是否可以多选
            openFileDlg.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);//设置默认路径为桌面
            openFileDlg.Filter = "图片文件|*.jpg|BMP图片|*.bmp|Gif图片|*.gif"; //设置多文件格式

            curFilename = openFileDlg.FileName;//地址
            if (openFileDlg.ShowDialog() == DialogResult.OK)
            {
                String filePath = openFileDlg.FileName;//设置路径
                this.pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
                this.pictureBox1.ImageLocation = filePath;//选择照片后在PictureBox中显示
                try
                {
                    curbitmap = (Bitmap)Image.FromFile(curFilename);//将公共变量curbitmap赋值
                }
                catch (Exception exp)
                {
                    MessageBox.Show(exp.Message);//如果出错显示错误信息
                }

            }
            //处理图片
            //以锐化效果显示图像
            try
            {
                int Height = this.pictureBox1.Image.Height;
                int Width = this.pictureBox1.Image.Width;
                Bitmap newBitmap = new Bitmap(Width, Height);
                Bitmap oldBitmap = (Bitmap)this.pictureBox1.Image;
                Color pixel;
                //拉普拉斯模板
                int[] Laplacian = { 1, 1, -1, 1, 8, 1, 1, 1, 1 };
                for (int x = 1; x < Width - 1; x++)
                    for (int y = 1; y < Height - 1; y++)
                    {
                        int r = 0, g = 0, b = 0;
                        int Index = 0;
                        for (int col = -1; col <= 1; col++)
                            for (int row = -1; row <= 1; row++)
                            {
                                pixel = oldBitmap.GetPixel(x + row, y + col); r += pixel.R * Laplacian[Index];
                                g += pixel.G * Laplacian[Index];
                                b += pixel.B * Laplacian[Index];
                                Index++;
                            }
                        //处理颜色值溢出
                        r = r > 255 ? 255 : r;
                        r = r < 0 ? 0 : r;
                        g = g > 255 ? 255 : g;
                        g = g < 0 ? 0 : g;
                        b = b > 255 ? 255 : b;
                        b = b < 0 ? 0 : b;
                        newBitmap.SetPixel(x - 1, y - 1, Color.FromArgb(r, g, b));
                    }
                //展示
                this.pictureBox1.Image = newBitmap; this.pictureBox2.SizeMode = PictureBoxSizeMode.StretchImage;//将图片处理后的大小显示在框中
                this.pictureBox2.Image = newBitmap;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "信息提示");
            }

        }
    }
}
